import React, {useRef, useState} from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table';
import { Link, connect,history } from 'umi';
import { queryArchiveList, saveAndUpdateEmployeeInfoList,loginMember } from '../service';
import { Divider, Button, message } from 'antd';
import moment from 'moment'
import ImportUser from "@/pages/System/RoleAssignment/components/ImportUser";
import {encryptPassword} from '@/services/user';

const TableList = ({currentUser}) => {
  const actionRef = useRef();
  const { authorities } = currentUser;
  const [loading, setLoading] = useState(false);
  const [importVisible, setImportVisible] = useState(false);

  const itemEdit = (item) => {
    history.push(`/info/edit/${item.oid}`)
  };

  const itemDetail = (item) => {
    history.push(`/info/details/${item.oid}`)
  };

  const addUser = () => {
    setImportVisible(true);
  };

  const handleImportCancel = () => {
    setImportVisible(false);
  };

  const saveUser = (values) => {
    setLoading(true);
    saveAndUpdateEmployeeInfoList(values).then(res => {
      if (res.flag) {
        setImportVisible(false);
        actionRef.current.reload();
        message.success("操作成功！");
      } else {
        message.error(res.error);
      }
      setLoading(false)
    });
  };

  const jumpLoginMember = (oid) => {
    loginMember(encryptPassword(oid)).then(res => {
      if(res.flag){
        message.success('操作成功！');
        window.open(res.data);
      }else{
        message.error(`获取自助端信息失败:` + res.error);
      }
    });
  };

  const columns = [
    {
      title: '工号',
      dataIndex: 'code',
      hideInSearch: true,
    },
    {
      title: '姓名',
      dataIndex: 'name',
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '公司',
      dataIndex: 'orgname',
    },
    {
      title: '部门',
      dataIndex: 'unitname',
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
    },
    {
      title: '入职时间',
      hideInSearch: true,
      dataIndex: 'hiredate',
      render: (text) => {
        return text !== '-' ? moment(text).format('YYYY-MM-DD') : ''
      }
    },
    {
      title: '司龄',
      hideInSearch: true,
      dataIndex: 'workingYears',
      render: (text,record) => {
        return ['3', '4', '5', '6'].indexOf(record.employeestatus) > -1 ? moment(record.dimissionTime).diff(record.hiredate, 'years',true).toFixed(1) + '年' : moment().diff(record.hiredate, 'years',true).toFixed(1) + '年';
      }
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      align:'center',
      width:'15%',
      render: (item, record) => (
        <>
          <a onClick={() => itemDetail(record)}>查看详情</a>
          {(authorities.indexOf("user:info:edit") > -1) && <><Divider type="vertical" /> <a onClick={() => itemEdit(record)}>修改</a></>}
          {
            (authorities.indexOf("user:member:login") > -1) && <>
              <Divider type="vertical" />
              <a onClick={() => jumpLoginMember(record.oid)}>登录自助</a>
            </>
          }
        </>
      ),
    },
  ];

  return (
    <PageHeaderWrapper>
        <ProTable
          actionRef={actionRef}
          rowKey="oid"
          request={(params) => queryArchiveList(params)}
          columns={columns}
          toolBarRender={(action, {selectedRows}) => [
            <Button key='0' type="primary" onClick={() => addUser(null)}>导入用户信息</Button>,
          ]}
        />
      <ImportUser
        visible={importVisible}
        loading={loading}
        onCancel={handleImportCancel}
        onSubmit={saveUser}
      >
      </ImportUser>
    </PageHeaderWrapper>
  )
}

export default connect(({user}) => ({
  currentUser: user.currentUser,
}))(TableList)
